package com.sky.service.impl;

import com.sky.context.BaseContext;
import com.sky.dto.ShoppingCartDTO;
import com.sky.entity.Dish;
import com.sky.entity.Setmeal;
import com.sky.entity.ShoppingCart;
import com.sky.mapper.DishMapper;
import com.sky.mapper.SetmealMapper;
import com.sky.mapper.ShoppingCartMapper;
import com.sky.service.ShoppingCartService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

@Service
@Slf4j
@RequiredArgsConstructor
public class ShoppingCartServiceImpl implements ShoppingCartService {

    private final ShoppingCartMapper shoppingCartMapper;
    private final DishMapper dishMapper;//菜品
    private final SetmealMapper setmealMapper;//套餐
    /**
     * 添加购物车
     * @param shoppingCartDTO
     */
    @Override
    public void addShoppingCart(ShoppingCartDTO shoppingCartDTO) {
        //判断当前加入到购物车中的商品是否已经存在
        ShoppingCart shoppingCart = new ShoppingCart();
        BeanUtils.copyProperties(shoppingCartDTO,shoppingCart);//把传入的值拷贝
        Long userId = BaseContext.getCurrentId();//获取当前微信登录的id
        shoppingCart.setUserId(userId);//存入userid

        List<ShoppingCart> list = shoppingCartMapper.list(shoppingCart);

        //如果已经存在，只需要将购物车数量+1
        if (list!=null && list.size()>0){
            ShoppingCart cart = list.get(0);//获得当前菜品或套餐
            cart.setNumber(cart.getNumber()+1);//给当前数量+1
            //再执行update修改数量+1
            shoppingCartMapper.updateNumberById(cart);
        }
        else {
            //如果不存在，需要插入一条购物车数据

            //判断传入的是菜品还是套餐，根据传入的dishId,跟setmealId是否为空来判断
            Long dishId = shoppingCartDTO.getDishId();
            if(dishId!=null){
                //不等于空，则传入的是菜品，反之则为套餐
                Dish dish = dishMapper.getById(dishId);//根据id查询菜品
                shoppingCart.setName(dish.getName());//设置菜品名称
                shoppingCart.setImage(dish.getImage());//设置菜品图片路径
                shoppingCart.setAmount(dish.getPrice());//设置菜品价格
            }else {
                //添加到购物车的是套餐
                Setmeal setmeal = setmealMapper.getById(shoppingCartDTO.getSetmealId());//根据id查询套餐
                shoppingCart.setName(setmeal.getName());//设置菜品名称
                shoppingCart.setImage(setmeal.getImage());//设置菜品图片路径
                shoppingCart.setAmount(setmeal.getPrice());//设置菜品价格
            }
            shoppingCart.setNumber(1);//设置数量为1
            shoppingCart.setCreateTime(LocalDateTime.now());//设置当前时间
            shoppingCartMapper.insert(shoppingCart);
        }
    }

    /**
     * 查看购物车
     * @return
     */
    @Override
    public List<ShoppingCart> showShoppingCart() {
        //获取当前微信用户id
        Long userId = BaseContext.getCurrentId();
        //构造对象
        ShoppingCart shoppingCart = ShoppingCart.builder()
                .userId(userId)
                .build();
        List<ShoppingCart> list = shoppingCartMapper.list(shoppingCart);
        return list;
    }

    /**
     * 清空购物车
     */
    @Override
    public void cleanShoppingCart() {
        shoppingCartMapper.deleteByUserId(BaseContext.getCurrentId());
    }
}
